#include "laurin-utils.h"
#include <netinet/ether.h>
#include <pcap.h>




const char* getReadableIP(u_int32_t ip, bool resolve)
{
	return libnet_addr2name4(ip, (resolve) ? LIBNET_RESOLVE : LIBNET_DONT_RESOLVE);
}


const char* getReadableMAC(u_int8_t mac[ETH_ALEN])
{
	// from netinet/ether.h
	return  ether_ntoa((struct ether_addr*)mac);
}


bool areMACEquals(u_int8_t* op1, u_int8_t* op2)
{
	if(!op1 || !op2 )
		return false;

	for(int i = 0; i < ETH_ALEN; i++)
		if(*(op1 + i) != *(op2 + i))
			return false;

	return true;
}


std::list<Glib::ustring>* getAvaileDeviceForLaurin()
{
	pcap_if_t* devs, *dev;
	char err[PCAP_ERRBUF_SIZE];

	std::list<Glib::ustring>* list = new std::list<Glib::ustring>();

	if(pcap_findalldevs(&devs, err) == 0)
	{
		for(dev = devs; dev; dev = dev->next)
		{
			if(dev->flags & PCAP_IF_LOOPBACK)
				continue;
			else
			{
				list->push_front(dev->name);

			}
		}

		pcap_freealldevs(devs);
	}

	return list;
}


u_int8_t* generatePreviousMAC(u_int8_t* mac)
{
	u_int8_t* tmp = (u_int8_t*) malloc(ETH_ALEN);
	memcpy(tmp, mac, ETH_ALEN);
	tmp[5] = tmp[5] - 1;

	return tmp;
}
